package com.zerdoor.controller;

import com.octo.captcha.service.CaptchaServiceException;
import com.zerdoor.model.UserInfo;
import com.zerdoor.service.LoginService;
import com.zerdoor.service.RegisterService;
import com.zerdoor.util.MD5;
import com.zerdoor.util.yz.CaptchaServiceSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Project: acg
 * Module ID: 002
 * Comments: 首页显示
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaoji
 * Create Date：2014-10-30
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @RequestMapping(value = "/toRegister", method = RequestMethod.GET)
    public String toRegister(HttpServletRequest request,Model model){
        model.addAttribute("presenter",request.getParameter("id"));
        return "/register/register";
    }

    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String result(HttpServletRequest request,Model model){
        return "/register/result";
    }
    /**
     * FunName:           register
     * Description :      注册
     * @return String：返回跳转页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> register(UserInfo userInfo,String verifcode,String repeatPassword,
                                        HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();

        //验证码
//        if(!equVerifCode(verifcode, request)){
//            map.put("status",-5);
//            return map;
//        }
        //账号确认
        map.put("username", userInfo.getUserName());
        map.put("phone", userInfo.getPhone());
        map.put("email", userInfo.getEmail());
        //密码确认
        if(userInfo.getPsd() == null){//密码验证
            map.put("status",-2);
            return map;
        }
        //确认密码确认
        if(!userInfo.getPsd().equals(repeatPassword)){//输入密码与确认密码不一致
            map.put("status",-3);
            return map;
        }else{
            map.put("password",MD5.getMd5String(userInfo.getPsd()));
        }
        //推荐者
        map.put("presenter", userInfo.getPresenter());

        map = registerService.addUserInfo(map);
        if((Integer)map.get("status") == 0){//添加成功后默认登陆
            UserInfo user = registerService.getTuserByName(userInfo.getUserName());
            request.getSession().setAttribute("curUser",user);
        }
        return map;
    }
    /**
     * 验证码验证
     *
     * @param verifcode
     * @param request
     * @return
     */
    public boolean equVerifCode(String verifcode, HttpServletRequest request) {
        Boolean isResponseCorrect = Boolean.FALSE;
        String captchaId = request.getSession().getId();
        try {
            isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, verifcode);
            if (isResponseCorrect) {
                return true;
            } else {
                return false;
            }
        } catch (CaptchaServiceException e) {
            return false;
        }
    }

    /**
     * FunName:           checkUserName
     * Description :      注册
     * @return String：返回跳转页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/checkUserName", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkUserName(@RequestParam("userName")  String userName){
        Map<String, Object> map = new HashMap<String, Object>();
        if("".equals(userName)) return false;
        Integer num = registerService.getUserInfoByUserName(userName);
        if(num==0){
            return true;
        }else{
           return false;
        }
    }

    /**
     * FunName:           checkUserPhone
     * Description :      注册
     * @return String：返回跳转页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/checkUserPhone", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkUserPhone(@RequestParam("phone")  String phone){
        Map<String, Object> map = new HashMap<String, Object>();
        if("".equals(phone)) return false;
        Integer num = registerService.getUserInfoByPhone(phone);
        if(num==0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * FunName:           checkUserEmail
     * Description :      注册
     * @return String：返回跳转页面
     * @Author:       xiaoji
     * @Create Date: 2014-10-27
     */
    @RequestMapping(value = "/checkUserEmail", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkUserEmail(@RequestParam("email")  String email){
        Map<String, Object> map = new HashMap<String, Object>();
        if("".equals(email)) return false;
        Integer num = registerService.getUserInfoByEmail(email);
        if(num==0){
            return true;
        }else{
            return false;
        }
    }
}
